package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.entity.StatusCode;
import com.itheima.pojo.Spec;
import com.itheima.service.SpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/spec")
//@CrossOrigin
public class SpecController {

    @Autowired
    private SpecService specService;

    @GetMapping("/findAll")
    public Result<List<Spec>> findAll() {
        List<Spec> list = specService.list();
        if (list.size()>0){
            return new Result<>(true, StatusCode.OK, "", list);
        }
        return new Result<>(false, StatusCode.ERROR, "", null);
    }

    @PostMapping("/add")
    public Result<List<Spec>> add(@RequestBody Spec spec) {
        boolean a = specService.save(spec);
        if (a) {
            return new Result<List<Spec>>(true, StatusCode.OK, "添加成功", null);
        }
        return new Result<List<Spec>>(false, StatusCode.ERROR, "添加失敗", null);
    }

    @GetMapping("/search/{page}/{size}")
    public Result findPage(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        LambdaQueryWrapper<Spec> wrapper = Wrappers.lambdaQuery(Spec.class);
        IPage<Spec> specPage = new Page<>(page, size);
        specService.page(specPage, wrapper);
        PageResult pageResult = new PageResult(specPage.getTotal(), specPage.getRecords());
        return new Result(true, StatusCode.OK, "", pageResult);
    }

    @PostMapping("/search/{page}/{size}")
    public Result searchPage(@PathVariable("page") Integer page,
                                 @PathVariable("size") Integer size,
                                 @RequestBody Spec searchMap) {
        LambdaQueryWrapper<Spec> wrapper = Wrappers.lambdaQuery(Spec.class);
        IPage<Spec> specPage = new Page<>(page, size);
        if (searchMap.getId() != null) {
            wrapper.eq(Spec::getId, searchMap.getId());
        }

        if (searchMap.getName() != null&&searchMap.getName().trim().equals("")) {
            wrapper.eq(Spec::getName, searchMap.getName());
        }

        if (searchMap.getOptions() != null&&searchMap.getOptions().trim().equals("")) {
            wrapper.eq(Spec::getOptions, searchMap.getOptions());
        }

        if (searchMap.getSeq() != null) {
            wrapper.eq(Spec::getSeq, searchMap.getSeq());
        }

        specService.page(specPage, wrapper);
        PageResult pageResult = new PageResult(specPage.getTotal(), specPage.getRecords());
        return new Result(true, StatusCode.OK, "", pageResult);
    }

    //findById
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        Spec spec = specService.getById(id);
        if (spec != null) {
            return new Result(true, StatusCode.OK, "查詢成功", spec);

        }
        return new Result(false, StatusCode.ERROR, "未查到", spec);
    }

    //update
    @PutMapping("/{id}")
    public Result update(@RequestBody Spec spec,
                         @PathVariable("id") Integer id){
        LambdaUpdateWrapper<Spec> wrapper = Wrappers.lambdaUpdate(Spec.class);
        wrapper.eq(Spec::getId,id);
        boolean b = specService.update(spec,wrapper);
        if (b){
            return new Result(true, StatusCode.OK, "更新成功", "yes");
        }
        return new Result(false, StatusCode.ERROR, "更新失敗", "no");
    }

    //delete
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id){
        boolean d = specService.removeById(id);
        if (d){
            return new Result(true, StatusCode.OK, "刪除成功", "yes");
        }
        return new Result(false, StatusCode.ERROR, "刪除失敗", "no");
    }
}
